package neu.edu.cn.nuemall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import neu.edu.cn.nuemall.dto.BooksTypeDTO;
import neu.edu.cn.nuemall.dto.BooksTypeGroupDTO;

import neu.edu.cn.nuemall.entity.BooksType;
import neu.edu.cn.nuemall.entity.BooksTypeGroup;
import neu.edu.cn.nuemall.mapper.BooksTypeGroupMapper;
import neu.edu.cn.nuemall.mapper.BooksTypeMapper;
import neu.edu.cn.nuemall.service.BooksTypeGroupService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BooksTypeGroupServiceImpl implements BooksTypeGroupService {

    @Autowired
    private BooksTypeGroupMapper booksTypeGroupMapper;

    @Autowired
    private BooksTypeMapper booksTypeMapper;

    @Override
    public List<BooksTypeGroupDTO> listAllBooksTypeGroup(String publishId) {
        QueryWrapper<BooksTypeGroup> queryWrapper=new QueryWrapper<BooksTypeGroup>();
        queryWrapper.eq("belong_publish_id",publishId);
        List<BooksTypeGroup> booksTypeGroupList= booksTypeGroupMapper.selectList(queryWrapper);

        List<BooksTypeGroupDTO> groupDtos = booksTypeGroupList.stream().map(booksTypeGroup -> {
            BooksTypeGroupDTO booksTypeGroupDTO = new BooksTypeGroupDTO();
            BeanUtils.copyProperties(booksTypeGroup, booksTypeGroupDTO);
            List<BooksTypeDTO> booksTypeDTOS = getBooksTypeByPublishIdAndGroupId(publishId, booksTypeGroup.getId());
            booksTypeGroupDTO.setBooksTypeList(booksTypeDTOS);
            return booksTypeGroupDTO;
        }).collect(Collectors.toList());
        return groupDtos;
    }

    private List<BooksTypeDTO> getBooksTypeByPublishIdAndGroupId(String publishId, String groupId) {
        // 查询商品分类列表
        QueryWrapper<BooksType> booksTypeQueryWrapper = new QueryWrapper<>();
        booksTypeQueryWrapper.eq("belong_publish_id", publishId);
        booksTypeQueryWrapper.eq("belong_group_id", groupId);
        List<BooksType> booksTypes = booksTypeMapper.selectList(booksTypeQueryWrapper);

        List<BooksTypeDTO> booksTypeList =booksTypes.stream().map(booksType -> {
            BooksTypeDTO booksTypeDTO = new BooksTypeDTO();
            BeanUtils.copyProperties(booksType, booksTypeDTO);
            return booksTypeDTO;
        }).collect(Collectors.toList());

        return booksTypeList;
    }
}
